﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestComplexNumbers
{
    class ComplexNumber
    {
        private int real;
        private int imaginary;

        public ComplexNumber(int r, int i)
        {
            real = r;
            imaginary = i;
        }

        public static ComplexNumber operator +(ComplexNumber f1, ComplexNumber f2)
        {

            int realPart = f1.real + f2.real;
            int imaginaryPart = f1.imaginary + f2.imaginary;

            return new ComplexNumber(realPart, imaginaryPart);
        }

        public static ComplexNumber operator -(ComplexNumber f1, ComplexNumber f2)
        {

            int realPart = f1.real - f2.real;
            int imaginaryPart = f1.imaginary - f2.imaginary;

            return new ComplexNumber(realPart, imaginaryPart);
        }

        public static ComplexNumber operator *(ComplexNumber f1, ComplexNumber f2)
        {

            int realPart = (f1.real * f2.real) - (f1.imaginary * f2.imaginary);
            int imaginaryPart = (f1.imaginary * f2.real) + (f1.real * f2.imaginary);

            return new ComplexNumber(realPart, imaginaryPart);
        }

        public static ComplexNumber operator /(ComplexNumber f1, ComplexNumber f2)
        {
            if ((f2.real * f2.real + f2.imaginary * f2.imaginary) != 0)
            {
                int realPart = ((f1.real * f2.real) + (f1.imaginary * f2.imaginary)) / (f2.real * f2.real + f2.imaginary * f2.imaginary);
                int imaginaryPart = ((f1.imaginary * f2.real) - (f1.real * f2.imaginary)) / (f2.real * f2.real + f2.imaginary * f2.imaginary);

                return new ComplexNumber(realPart, imaginaryPart);
            }
            else
            {
                throw new Exception();
            }
        }

        public static explicit operator double(ComplexNumber complexNumber)
        {
            if (complexNumber.imaginary != 0)
            {
                return double.NaN;
            }
            else
            {
                return (double)complexNumber.real;
            }
        }

        public static implicit operator ComplexNumber(double value)
        {
            return new ComplexNumber((int)value, 0);
        }


        public override string ToString()
        {
            return String.Format("{0}+{1}i", real, imaginary);
        }

        public int this[int index]
        {
            get
            {
                if (index >= 0 && index <= 1)
                {
                    if (index == 0)
                        return this.real;
                    else
                        return this.imaginary;
                }
                else
                {
                    throw new IndexOutOfRangeException(String.Format("Index {0} is invalid!", index));
                }

            }        

        }
    }
}
